package com.xiaohui.gwc.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xiaohui.gwc.mapper.Shopping_cartMapper;
import com.xiaohui.gwc.mode.*;
import com.xiaohui.gwc.service.Shopping_cartService;
import com.xiaohui.gwc.utils.SnowIdUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Poppin
 * @Date: 2021/11/30 12:43
 * @Version: 1.0
 */
@Service
public class Shopping_cartServiceImpl implements Shopping_cartService {

    private Shopping_cartMapper shopping_cartMapper;

    private Shopping_cartServiceImpl(Shopping_cartMapper shopping_cartMapper){
        this.shopping_cartMapper = shopping_cartMapper;
    }

    /**
     * 添加购物车
     * @param shoppingC
     * @param jwtuser_id
     * @return
     */
    @Override
    public Result addShoppingCart(ShoppingC shoppingC, Integer jwtuser_id) {
        QueryWrapper<Shopping_cart> queryWrapper = new QueryWrapper();
        queryWrapper.eq("prod_id",shoppingC.getProd_id())
                .eq("jwtuser_id",jwtuser_id);
        Shopping_cart shopping_cart1 = new Shopping_cart();
        shopping_cart1 = shopping_cartMapper.selectOne(queryWrapper);
        if (shopping_cart1==null){
            Shopping_cart shopping_cart = new Shopping_cart();
            shopping_cart.setSc_id(SnowIdUtils.uniqueLongHex());
            shopping_cart.setJwtuser_id(jwtuser_id);
            shopping_cart.setNumber(shoppingC.getNumber());
            shopping_cart.setProd_id(shoppingC.getProd_id());
            int result = shopping_cartMapper.insert(shopping_cart);
            if (result == 1){
                return new Result<AffectedRows>(
                        ReturnCode.SUCCESS.getCode(),
                        ReturnCode.SUCCESS.getType(),
                        "添加成功",new AffectedRows(result)
                );
            }else {
                return new Result<AffectedRows>(
                        ReturnCode.ERROR.getCode(), ReturnCode.ERROR.getType(),
                        "添加失败",new AffectedRows(result)
                );
            }
        }else {
            UpdateWrapper<Shopping_cart> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("sc_id",shopping_cart1.getSc_id())
                    .eq("jwtuser_id",shopping_cart1.getJwtuser_id())
                    .eq("prod_id",shopping_cart1.getProd_id())
                    .set("number",shopping_cart1.getNumber() + shoppingC.getNumber());
            int result = shopping_cartMapper.update(null,updateWrapper);
            if (result==1){
                return new Result<AffectedRows>(
                        ReturnCode.SUCCESS.getCode(),
                        ReturnCode.SUCCESS.getType(),
                        "添加成功",new AffectedRows(result)
                );
            }
            else {
                return new Result<AffectedRows>(
                        ReturnCode.ERROR.getCode(), ReturnCode.ERROR.getType(),
                        "添加失败",new AffectedRows(result)
                );
            }
        }
    }

    /**
     * 购物车修改
     * @param shoppingCart_update
     * @param jwtuser_id
     * @return
     */
    @Override
    public Result updateShoppingCart(ShoppingCart_Update shoppingCart_update, Integer jwtuser_id) {

        UpdateWrapper<Shopping_cart> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("sc_id",shoppingCart_update.getSc_id())
                .eq("jwtuser_id",jwtuser_id)
                .eq("prod_id",shoppingCart_update.getProd_id())
                .set("number",shoppingCart_update.getNumber());

        int result = shopping_cartMapper.update(null,updateWrapper);
        if (result == 1){
            return new Result<AffectedRows>(
                    ReturnCode.SUCCESS.getCode(),
                    ReturnCode.SUCCESS.getType(),
                    "修改成功",new AffectedRows(result)
            );
        }else {
            return new Result<AffectedRows>(
                    ReturnCode.ERROR.getCode(), ReturnCode.ERROR.getType(),
                    "修改失败",new AffectedRows(result)
            );
        }
    }

    /**
     * 删除购物车
     * @param sc_id
     * @param jwtuser_id
     * @return
     */
    @Override
    public Result deleteShoppingCart(String sc_id, Integer jwtuser_id) {

        Map<String,Object> deleteMap = new HashMap<>();
        deleteMap.put("sc_id",sc_id);
        deleteMap.put("jwtuser_id",jwtuser_id);
        int result = shopping_cartMapper.deleteByMap(deleteMap);
        if (result > 0){
            return new Result<AffectedRows>(
                    ReturnCode.SUCCESS.getCode(),
                    ReturnCode.SUCCESS.getType(),
                    "删除成功",new AffectedRows(result)
            );
        }else {
            return new Result<AffectedRows>(
                    ReturnCode.ERROR.getCode(), ReturnCode.ERROR.getType(),
                    "删除失败",new AffectedRows(result)
            );
        }
    }

    /**
     * 查询购物车
     * @param jewuser_id
     * @return
     */
    @Override
    public Result QueryShoppingCart(Integer jewuser_id) {
        List<ShopCart> shoppingCarts;
        shoppingCarts = shopping_cartMapper.getAllShopCart(jewuser_id);

        return new Result<List>(
                ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getType(), "查询成功",shoppingCarts
        );
    }

    @Override
    public Result Recomne(Integer jwtuser_id) {
        List<Products> productsList;
        productsList= shopping_cartMapper.getRecomn(jwtuser_id);
        return new Result<List>(
                ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getType(), "查询成功",productsList
        );
    }
}

